/*
 * Copyright @ 2024 weili23
 * com.example.mythread.a10threadExerciseLottery 10:19
 * All right reserved.
 */

package com.example.mythread.a10threadExerciseLottery;


import java.util.ArrayList;
import java.util.Collections;

/**
 * @desc: 抽奖箱-抽完在打印，计算最大值，总和
 * @author: weili23
 * @createTime: 2024/10/22 10:19
 * @version: v1.0
 */
public class MyLottery1 extends Thread {
    ArrayList<Integer> list;
    public MyLottery1(ArrayList<Integer> list){
        this.list = list;
    }
    @Override
    public void run() {
        ArrayList<Integer> boxList = new ArrayList<>();
        while (true){
            synchronized (MyLottery1.class){
                if(list.size() ==0){
                    if(boxList.size() !=0){
                        System.out.println(getName()+"的奖："+boxList);
                        Collections.sort(boxList);
                        Integer integer = boxList.get(boxList.size() - 1);
                        System.out.println(getName()+"最大的值是："+integer+"元大奖");
                        // 使用stream计算和
                        int sum = boxList.stream().mapToInt(Integer::intValue).sum();
                        System.out.println(getName()+"的奖和："+sum);

                    }

                    break;
                }else{
                    //随机打乱，取第一个
                    Collections.shuffle(list);
                    Integer integer = list.get(0);
                    boxList.add(integer);
                    list.remove(0);
                    //System.out.println(getName()+"又产生了一个"+integer+"元大奖");
                }
            }
        }
    }
}
